#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
string zimu[26];
int main() {
	cin.sync_with_stdio(false);
	char tmp;
	for (int i=0; i<26; ++i) {
		cin>>tmp>>zimu[i];
	}
	int n;
	map<string, string> mapp;
	string s,z;
	cin>>n;
	for (int i=0; i<n; ++i) {
		cin>>s;
		int len=s.length();
		z="";
		for (int j=0; j<len; ++j) {
			z+=zimu[s[j]-'A'];
		}
		mapp[z]=s;
	}
	while (cin>>n && n!=0) {
		bool flag=true;
		vector<string> res;
		for (int i=0; i<n; ++i) {
			cin>>s;
			if (!mapp.count(s) && flag) {
				flag=false;
				z=s;
			}
			if(mapp.count(s) && flag) {
				res.push_back(mapp[s]);
			}
		}
		if (!flag) cout<<z<<" not in dictionary."<<endl;
		else {
			int len=res.size();
			for (int i=0; i<len-1; ++i) {
				cout<<res[i]<<' ';
			}
			cout<<res[len-1]<<endl;
		}
	}
	return 0;
}
